<?php
namespace App\Controllers\Project;

use App\Controllers\BaseController;
use App\Models\ProjectSmsLog;
use App\Models\Project;
use App\Library\Helper;

class SmsController extends BaseController {

    public function initialize() {
        parent::initialize();
        if($this->request->isPost()) {
            $this->checkLogin();
        }
    }

    //---------------------------------------------------------------------------------
    //短信营销列表
    //---------------------------------------------------------------------------------
    public function indexAction() {
        $this->display('/project/sms/index');
    }

    //---------------------------------------------------------------------------------
    //获取短信营销的记录
    //---------------------------------------------------------------------------------
    public function getsAction() {
        $data = [
            'page' => $this->request->getPost('page'),
            'size' => $this->request->getPost('size'),
        ];

        if($data['page'] == '' || !is_numeric($data['page'])) {
            return $this->sendAjax(false, '获取数据失败，未获取到当前的页数！');
        }
        if($data['size'] == '' || !is_numeric($data['size'])) {
            return $this->sendAjax(false, '获取数据失败，未获取到当前指定每页条数！');
        }
        $data['page'] = intval($data['page']);
        $data['size'] = intval($data['size']);

        $offset   = ($data['page'] - 1) * $data['size'];
        $log      = new ProjectSmsLog();
        $project  = new Project();
        $ret      = $log->gets($this->parseFilter(), $offset, $data['size']);
        $projects = $project->getIdAndName();
        foreach($ret as $k => $v) {
            foreach($projects as $k1 => $v1) {
                if($v['project_id'] == $v1['id']) {
                    $ret[$k]['project_name'] = $v1['name'];
                    break;
                }
            }
        }
        if($ret !== false) {
            return $this->sendAjax(true, $ret);
        } else {
            return $this->sendAjax(false, "获取数据失败，系统错误！");
        }
    }

    //---------------------------------------------------------------------------------
    //统计短信营销的总数
    //---------------------------------------------------------------------------------
    public function countAction() {
        $log = new ProjectSmsLog();
        $ret = $log->total($this->parseFilter());
        if($ret !== false) {
            return $this->sendAjax(true, $ret);
        } else {
            return $this->sendAjax(false, "获取总数失败，系统错误！");
        }
    }

    //---------------------------------------------------------------------------------
    //短信营销条件筛选
    //---------------------------------------------------------------------------------
    private function parseFilter() {
        $where = $this->request->getPost('filter');
        if($where['mobile'] == '') {
            unset($where['mobile']);
        }
        if($where['status'] == '' || !is_numeric($where['status'])) {
            unset($where['status']);
        } else {
            $where['status'] = intval($where['status']);
        }
        if($where['project_id'] == '' || !is_numeric($where['project_id'])) {
            unset($where['project_id']);
        } else {
            $where['project_id'] = intval($where['project_id']);
        }
        if($where['start_date'] != '') {
            $where['start_date'] = Helper::getDatepickerDateToStandardDate($where['start_date']);
        } else {
            unset($where['start_date']);
        }
        if($where['end_date'] != '') {
            $where['end_date'] = Helper::getDatepickerDateToStandardDate($where['end_date']);
        } else {
            unset($where['end_date']);
        }
        return $where;
    }
}
